/**
 * Created with IntelliJ IDEA.
 * User: suntao
 * Date: 16-9-28
 * Time: 下午12:38
 * To change this template use File | Settings | File Templates.
 */
$(function () {

    $("#pair_add").click(function () {
        add("添加配对");
    });

    // 编辑广告 点击事件
    $(".pair_edit").click(function() {
        var pair_id =  $(this).parent().parent().children("td.pair_id").text();
        edit("编辑配对", pair_id);
    });
    // 发布广告
    $(".release").click(function () {
        var pair_id =  $(this).parent().parent().children("td.pair_id").text();
        layer.confirm('确认要发布吗？', function (index) {
            status_manager(pair_id, 1);
        });

    });
    // 取消发布广告
    $(".cancel_release").click(function () {
        var pair_id =  $(this).parent().parent().children("td.pair_id").text();
        layer.confirm('确认要取消发布吗？', function (index) {
            status_manager(pair_id, 0);
        });

    });

    // 点击删除
    $(".opt_del").click(function () {
        var $del = $(this);
        var pair_id =  $(this).parent().parent().children("td.pair_id").text();
        layer.confirm('确认要删除吗？', function (index) {
            del($del, pair_id);
        });
    });
    // 批量删除
    $("#batch_del").click(function () {
        var ids = [];
        $("table").find(".pair_checkbox").each(function () {
            if ($(this).is(":checked") == true) {
                var id = $(this).parent().siblings(".pair_id").text();
                ids.push(id);
            }

        });
        if (ids.length > 0) {
            layer.confirm('确认删除已选中的广告吗？', function (index) {
                ad_batch_del(ids.join(","));
            });

        }
    });
});
// 添加广告
function add(title) {
    var index = layer.open({
        type: 2,
        title: title,
        content: "transDevicePairSNAddPage.do"
    });
    layer.full(index);
}

// 编辑广告
function edit(title, id) {
    var index = layer.open({
        type: 2,
        title: title,
        content: "transDevicePairSNEditPage.do?id=" + id
    });
    layer.full(index);
}

function status_manager(id, status) {
    $.ajax({
        url: "transDevicePairSNStatusUpdate.do",
        type: "post",
        async: true,
        data:"id=" + id + "&status=" + status,
        dataType: "json",
        success: function (data) {
            if (data["code"] == "1000") {
                location.replace(location.href);
            } else {
                layer.msg('操作失败!!', {icon: 5, time: 1000});
            }
        },
        error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
}
/*图片-删除*/
function del(obj, id) {
    $.ajax({
        url: "transDevicePairSNDelete.do",
        type: "post",
        async: true,
        data:"id=" + id,
        dataType: "json",
        success: function (data) {
            if (data["code"] == "1000") {
                location.replace(location.href);
//                $(obj).parents("tr").remove();
//                layer.msg('已删除!', {icon: 1, time: 1000});
            } else {
                layer.msg('删除失败!', {icon: 5, time: 1000});
            }
        },
        error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
//    layer.confirm('确认要删除吗？', function (index) {
//        $(obj).parents("tr").remove();
//        layer.msg('已删除!', {icon: 1, time: 1000});
//    });

}

function ad_batch_del(ids) {
    $.ajax({
        url: "adBatchDel.do",
        type: "post",
        async: true,
        data:"ids=" + ids,
        dataType: "json",
        success: function (data) {
            if (data["code"] == "1000") {
                location.replace(location.href);
//                $(obj).parents("tr").remove();
//                layer.msg('已删除!', {icon: 1, time: 1000});
            } else {
                layer.msg('删除失败!', {icon: 5, time: 1000});
            }
        },
        error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
}
